class Solution
{
public:
    int findMaxValueOfEquation(vector<vector<int>> &points, int k)
    {
        auto compare = [&](int i, int j)
        {
            return points[i][1] - points[i][0] < points[j][1] - points[j][0];
        };

        priority_queue<int, vector<int>, decltype(compare)> pq(compare);
        int result = INT_MIN;

        for (int i = 0; i < points.size(); ++i)
        {
            while (!pq.empty() && points[i][0] - points[pq.top()][0] > k)
            {
                pq.pop();
            }
            if (!pq.empty())
            {
                result = max(result, points[i][0] + points[i][1] + points[pq.top()][1] - points[pq.top()][0]);
            }
            pq.push(i);
        }
        return result;
    }
};